package com.liao.ten.image;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.liao.ten.R;
import com.liao.ten.base.BaseFragment;
import com.liao.ten.utils.ContentUtils;
import com.liao.ten.utils.FileUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ImageFragment extends BaseFragment implements ViewPager.OnPageChangeListener {

    private final int DATA1 = 1;
    private final int DATA2 = 2;

    private ViewPager fragVp;
    private ImageView top_iv_icon;
    private ImageView top_tv_data1;
    private ImageView top_tv_data2;
    private ImageView top_tv_week;
    private ImageView top_tv_divider;
    private ImageView top_tv_month;

    private Context context;

    private List<ImageBean.ResultBean> resultBeans;
    private List<ImageDetailInfo> imageDetailInfos;
    private List<Fragment> fragments;
    private ImageFragmentPagerAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_style, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        initTopData();
        if (isNetworkAvailable(context)) {
            showDialog(context);
            loadData(context, ContentUtils.PICTURE_URL, DATA1, "image.txt");
        } else {
            showData(FileUtil.getDataToFile(context, "image.txt"), DATA1);
        }
    }

    private void initTopData() {
        top_iv_icon.setImageResource(R.mipmap.logo_diagram);
    }

    private void initView(View view) {
        fragVp = (ViewPager) view.findViewById(R.id.frag_vp);
        top_iv_icon = view.findViewById(R.id.top_iv_icon);
        top_tv_data1 = view.findViewById(R.id.top_tv_data1);
        top_tv_data2 = view.findViewById(R.id.top_tv_data2);
        top_tv_week = view.findViewById(R.id.top_tv_week);
        top_tv_divider = view.findViewById(R.id.top_tv_divider);
        top_tv_month = view.findViewById(R.id.top_tv_month);

        fragments = new ArrayList<>();
        resultBeans = new ArrayList<>();
        imageDetailInfos = new ArrayList<>();
        fragVp.addOnPageChangeListener(this);
        context = getContext();
        adapter = new ImageFragmentPagerAdapter(getChildFragmentManager(), fragments);
        fragVp.setAdapter(adapter);
    }

    @Override
    public void showDataFromNet(String s, int id) {
        showData(s, id);
    }

    private void showData(String s, int id) {
        if (id == DATA1) {
            ImageBean imageBean = new Gson().fromJson(s, ImageBean.class);
            for (ImageBean.ResultBean resultBean : imageBean.getResult()) {
                int beanId = resultBean.getId();
                resultBeans.add(resultBean);
                if (isNetworkAvailable(context)) {
                    loadData(context, ContentUtils.PICTURE_DETATLS_URL + beanId, DATA2, 3);
                } else {
                    String data = FileUtil.getDataToDB("imageId", "imagedetailinfo", ImageDetailInfo.class, beanId);
                    showData1(data);
                }
            }
        } else if (id == DATA2) {
            showData1(s);
        }
    }

    private static final String TAG = "ImageFragment";

    private void showData1(String s) {
        Log.d(TAG, "showData1: ======" + s);
        ImageDetailInfo imageDetailInfo = new Gson().fromJson(s, ImageDetailInfo.class);
        imageDetailInfos.add(imageDetailInfo);
        if (imageDetailInfos.size() == 10) {
            for (int i = 0; i < imageDetailInfos.size(); i++) {
                ItemImageFragment itemArticleLayoutFragment = new ItemImageFragment();
                Bundle bundle = new Bundle();
                bundle.putSerializable("data1", resultBeans.get(i));
                bundle.putSerializable("data2", imageDetailInfos.get(i));
                itemArticleLayoutFragment.setArguments(bundle);
                fragments.add(itemArticleLayoutFragment);
            }
            dismissDialog();
            adapter.notifyDataSetChanged();
        }
    }

    private void setTopData() {
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DATE);
        int week = calendar.get(Calendar.DAY_OF_WEEK);
        int monthImg = ContentUtils.months[month];
        int weekImg = ContentUtils.weeks[week];
        top_tv_month.setImageResource(monthImg);
        top_tv_week.setImageResource(weekImg);
        if (day < 10) {
            top_tv_data1.setImageResource(ContentUtils.days[0]);
            top_tv_data2.setImageResource(ContentUtils.days[day]);
        } else {
            String s = String.valueOf(day);
            String substring = s.substring(0, 1);
            String substring1 = s.substring(1, 2);
            top_tv_data1.setImageResource(ContentUtils.days[Integer.parseInt(substring)]);
            top_tv_data2.setImageResource(ContentUtils.days[Integer.parseInt(substring1)]);
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


}
